package com.cq;

import java.util.List;

import com.cq.config.SpringMongoConfig;
import com.cq.encounter.RetailBossEncounter;
import com.cq.user.User;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
//import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

public class TestingClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		ApplicationContext context = 
				// Can only use ClassPathXmlApplicationContext when in a jar and file paths are not available
//	             new ClassPathXmlApplicationContext("Beans.xml");
				 new FileSystemXmlApplicationContext("src/main/resources/META-INF/Beans.xml");
		RetailBossEncounter obj = (RetailBossEncounter) context.getBean("retailbossencounter");

	    obj.getName();
		
		ApplicationContext ctx = 
				new AnnotationConfigApplicationContext(SpringMongoConfig.class);
	    
		MongoOperations mongoOperation = 
				(MongoOperations)ctx.getBean("mongoTemplate");
		
		User user = new User("mkyong", "password123");
		 
		  // save
		  mongoOperation.save(user, "users");
	 
		  // find
		  User savedUser = mongoOperation.findOne(
			new Query(
	                    Criteria.where("username").is("mkyong")
	                ), User.class,"users"
	          );
	 
		  System.out.println("savedUser : " + savedUser);
	 
		  // update
		  mongoOperation.updateMulti(
			new Query(
	                    Criteria.where("username").is("mkyong")
	                ),
	                Update.update("password", "new password"), "users"
	          );
	 
		  // find
		  User updatedUser = mongoOperation.findOne(
			new Query(
	                    Criteria.where("username").is("mkyong")
	                ), User.class,"users"
	          );
	 
		  System.out.println("updatedUser : " + updatedUser);
	 
		  // delete
		  mongoOperation.remove(
			new Query(
	                    Criteria.where("username").is("mkyong")
	                ), "users"
	          );
	 
		  // List
		  List<User> listUser = mongoOperation.findAll(User.class, "users");
		  System.out.println("Number of user = " + listUser.size());
		
		
	    
	}

}
